package com.ruoyi.test.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.test.mapper.MigrationMapper;
import com.ruoyi.test.vo.Migration;
import com.ruoyi.test.vo.MigrationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/migration")
public class EchartMapController {
    @Autowired
    private MigrationMapper migrationMapper;

    /**
     * 查询世界范围模拟迁徙数据
     * @return
     */
    @GetMapping("/data")
    public AjaxResult getMigrationData() {
        List<MigrationData> migrationDataList = migrationMapper.selectMigrationData();

        // 将数据转换为前端期望的格式
        Map<String, List<List<Migration>>> resultData = new HashMap<>();

        for (MigrationData data : migrationDataList) {
            String fromCity = data.getFromCity();
            String toCity = data.getToCity();
            int value = data.getValue();

            List<Migration> migrationPair = new ArrayList<>();
            migrationPair.add(new Migration(fromCity, 0));
            migrationPair.add(new Migration(toCity, value));

            if (!resultData.containsKey(fromCity)) {
                resultData.put(fromCity, new ArrayList<>());
            }
            resultData.get(fromCity).add(migrationPair);
        }

        // 打印数据
        for (String key : resultData.keySet()) {
            System.out.println("迁徙数据: " + key);
            for (List<Migration> pair : resultData.get(key)) {
                System.out.println(pair.get(0).getName() + " -> " + pair.get(1).getName() + " (" + pair.get(1).getValue() + ")");
            }
        }

        return AjaxResult.success(resultData);
    }
}
